import { Component, OnInit, ChangeDetectorRef } from '@angular/core';
import * as constants from '../../../providers/Constants'
import { NavController, AlertController, PopoverController } from '@ionic/angular';
import { ToastService } from './../../../providers/toast/toast.service'
import { Storage } from 'src/app/providers/Storage';
import { AuthService } from 'src/app/services/auth.service';
import { Utils } from 'src/app/providers/Utils';
import { ActivatedRoute, Params } from '@angular/router';
import { IssueComponentComponent } from 'src/app/component/issue-component/issue-component.component';

@Component({
  selector: 'app-register',
  templateUrl: './register.page.html',
  styleUrls: ['./register.page.scss'],
})
export class RegisterPage implements OnInit {
  // 验证码倒计时
  verifyCode: any = {
    verifyCodeTips: "Get code",
    countdown: constants.DEFAULT_SMS_TIME,
    disable: true
  }
  isCheck: boolean = false;
  // 验证码倒计时
  signInfo: any = {
    userPhone: "",
    code: "",
    userPassword: '',
  }
  pageType: number = 0;
  ngOnInit() {

  }

  constructor(private toast: ToastService,
    private alertController: AlertController,
    private ref: ChangeDetectorRef,
    private pop: PopoverController,
    private activeRoute: ActivatedRoute,
    public nav: NavController,
    public service: AuthService, ) {
    this.activeRoute.queryParams.subscribe((params: Params) => {
      this.pageType = params.pageType;
    });
  }
  register() {
    if (!this.signInfo.userPhone) {
      this.toast.show('The mailbox cannot be empty');
    } else if (!Utils.isEmail(this.signInfo.userPhone)) {
      this.toast.show('Mailbox format error');
    } else if (!this.signInfo.userPassWord) {
      this.toast.show('The password cannot be empty');
    } else if (!this.signInfo.email) {
      this.toast.show('The verification code cannot be empty');
    } else if (this.signInfo.userPassWord.length < 6) {
      this.toast.show('Password must not be less than 6 digits');
    } else {
      let data = {
        userPhone: this.signInfo.userPhone,
        userPassWord: this.signInfo.userPassWord,
        copyType: constants.COPY_TYPE,
        code: this.signInfo.code,
        email: this.signInfo.email,
      }
      this.service.register(data).subscribe(res => {
        // 保存成功返沪上一页，否则弹出提示框
        if (res.retCode == '000000') {
          this.toast.show("Registered successfully");
          Storage.localStorage.set('token', this.signInfo.userPhone);
          Storage.localStorage.set('userPhone', this.signInfo.userPhone);
          this.nav.navigateRoot('/login');
          // this.getUserInfo();
        } else if (constants.errCode[res.retCode]) {
          if (constants.errCode[res.retCode] != '0') {
            this.toast.show(constants.errCode[res.retCode]);
          }
        } else if (res.retMsg) {
          this.toast.show(res.retMsg);
        } else {
          this.toast.show("exception service routine");
        }
      });
    }
  }

  getSms() {
    if (!this.signInfo.userPhone) {
      this.toast.show('The mailbox cannot be empty');
    } else if (!Utils.isEmail(this.signInfo.userPhone)) {
      this.toast.show('Mailbox format error');
    } else {
      if (this.verifyCode.disable) {
        let data = {
          email: this.signInfo.userPhone,
          type: 0,
        }
        this.service.getSms(data).subscribe(res => {
          // 保存成功返沪上一页，否则弹出提示框
          if (res.retCode == '000000') {
            this.toast.show("successful");
            this.settime();
          } else {
            if (constants.errCode[res.retCode]) {
              if (constants.errCode[res.retCode] != '0') { this.toast.show(constants.errCode[res.retCode]); }
            } else if (res.retMsg) {
              this.toast.show(res.retMsg);
            } else {
              this.toast.show("Email delivery failed");
            }
          }
        });
      }
    }
  }
  // 倒计时
  settime() {
    if (this.verifyCode.countdown == 1) {
      this.verifyCode.countdown = constants.DEFAULT_SMS_TIME;
      this.verifyCode.verifyCodeTips = "Get code";
      this.verifyCode.disable = true;
      return;
    } else {
      this.verifyCode.disable = false;
      this.verifyCode.countdown--;
    }
    this.verifyCode.verifyCodeTips = "Get (" + this.verifyCode.countdown + ")";
    setTimeout(() => {
      this.verifyCode.verifyCodeTips = "Get (" + this.verifyCode.countdown + ")";
      this.settime();
    }, 1000);
  }
  onClickPsw($event) {
    this.presentPopover(event);
  }

  async presentPopover(ev: any) {
    const popover = await this.pop.create({
      // backdropDismiss: false,//点击背景消失：否		
      event: ev,
      mode: 'ios',
      cssClass: 'pop-class',
      // translucent: true,
      component: IssueComponentComponent,//要使用的popover组件
    }); //向popover组件传的值
    return await popover.present()
  }
}
